package com.witframework.remote;

/**
 * 对远程调用请求的封装
 * @author wuxinyang
 */
public class RemoteRequest implements java.io.Serializable,Cloneable{
	private static final long serialVersionUID = 1L;
	//目标地址，如果为localhost则为本地方法调用。
	private String serverAddress;
	private String serviceType="spring";
	//spring bean的配置名称，如果配置了该值则通过spring配置装载service。
	//服务的接口class名称，必须带包名
	private String serviceName;
	//要调用的方法名称
	private String methodName;
	//传入的参数列表
	private Object[] arguments;
	private boolean resultAsXml;
	private boolean argumentFromXml=false;
	/**
	 * serverAddress=localhost:8080
	 * serviceAddress=spring:service/getAll
	 * serviceAddress=class:service/getAll
	 */
	public RemoteRequest(String serviceAddress) {
		int typeIndex=serviceAddress.indexOf(":");
		if(typeIndex>0){
			serviceType=serviceAddress.substring(0,serviceAddress.indexOf(":"));
			this.serviceName=serviceAddress.substring(serviceAddress.lastIndexOf(":")+1,serviceAddress.lastIndexOf("/"));
		}else{
			this.serviceName=serviceAddress.substring(0,serviceAddress.indexOf("/"));
		}
		this.methodName=serviceAddress.substring(serviceAddress.lastIndexOf("/")+1);
	}

	public RemoteRequest(String serverAddress,String serviceAddress) {
		this(serviceAddress);
		this.serverAddress=serverAddress;
	}
	/**
	 * 清除参数列表
	 */
	public void clearArguments(){
		arguments=null;
	}
	public void setArgument(Object... args){
		arguments=args;
	}
	public void setArgumentFromXml(String... args){
		arguments=args;
		argumentFromXml=true;
	}
	public Object[] getArguments() {
		return arguments;
	}
	public String getMethodName() {
		return methodName;
	}
	public String getServerAddress() {
		return serverAddress;
	}
	public String getServiceName() {
		return serviceName;
	}
	public String getServiceType(){
		return serviceType;
	}
	@Override
	public Object clone() throws CloneNotSupportedException{
		return super.clone();
	}
	public void setServerAddress(String serverAddress) {
		this.serverAddress = serverAddress;
	}
	public void setServiceType(String serviceType) {
		this.serviceType = serviceType;
	}
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public boolean isResultAsXml() {
		return resultAsXml;
	}

	public void setResultAsXml(boolean resultAsXml) {
		this.resultAsXml = resultAsXml;
	}

	public boolean isArgumentFromXml() {
		return argumentFromXml;
	}
}
